package com.dragon.utilslibrary.dblib;



import com.dragon.utilslibrary.AppConfig;
import com.dragon.utilslibrary.dblib.clipher.SQLMMHelper;
import com.dragon.utilslibrary.dblib.dbutil.UsersUtil;
import com.dragon.utilslibrary.tools.FileUtils;

import java.io.File;


public enum PrivateDataBaseEnums {
	/**公共用户文件名*/
	user_db_name("user"),
	/**逻辑db文件名*/
	logic_db_name("logic"),
	/**备份文件夹*/
	backDb_file_name("backDb"),
	/**
	 * 存放本地数据库的路径
	 */
	databasefile( getDbFilePath() + "db");

	/**
	 * 文件存储的文件路径
	 */
	public String value;

	PrivateDataBaseEnums(String value) {
		this.value = value;
	}
	private static String getDbFilePath(){
		if(AppConfig.FLAG_DEBUGGABLE && DBConfig.is_db_file_sdcard){
			return FileUtils.getFolderPath();
		}else{
			return AppConfig.rootFile + "/";
		}
	}
	/**
	 * 获取与用户无关的数据库路径
	 * 
	 * @return
	 */
	public String getDefaultDBFile() {
		String path = null;
		path = getDataBasePath() + "/" + user_db_name.value + ".db";
		return path;
	}

	/**
	 * 获取与用户有关的数据库路径
	 * 
	 * @return
	 */
	public String getUserDBFile() {
		
		String path = null;
		String currentUserId = UsersUtil.getInstance().getCurrentUserId();
		if (currentUserId != null) {
			File fileUser = new File(getDataBasePath() + "/" + currentUserId);
			if (!fileUser.exists()) {
				fileUser.mkdirs();
			}
			path = fileUser + "/" + logic_db_name.value + ".db";
			return path;
		}
		return value;
	}
	/**
	 * 获取数据库路径
	 * @return
	 */
	public String getDataBasePath(){
		if(AppConfig.FLAG_DEBUGGABLE && DBConfig.is_db_file_sdcard){
			File file = new File(databasefile.value );
			if (!file.exists()) {
				file.mkdirs();
			}
			return file.getAbsolutePath();
		}else{
			if(AppConfig.mContext.getDatabasePath("tmp.db") == null){
				if ("1".equalsIgnoreCase(DBConfig.encryptionDB)) {
					//加密
					new SQLMMHelper(AppConfig.mContext, "tem.db");
				} else if ("0".equalsIgnoreCase(DBConfig.encryptionDB)) {
					//不加密
					new DBOpenHelper(AppConfig.mContext, "tmp.db");
				}
			}
			String dbFilePath = AppConfig.mContext.getDatabasePath("tmp.db")
					.getAbsolutePath().replace("tmp.db", "") + databasefile.value;
			File file = new File(dbFilePath);
			if (!file.exists()) {
				file.mkdirs();
			}
			return file.getAbsolutePath();
		}
	}
}
